using System;
using System.Text;
using System.Collections.Generic;
using DatastoreLib;

namespace DatastoreLibTest {
	class Tester {
		private ElevatorLogging el;
		
		public static void Main (string[] args) {
			Tester tester = new Tester();
			
			// add entry's that should work
			tester.addFilledEntriesTest("TestTypeA");
			tester.addFilledEntriesTest("TestTypeB");
			tester.addFilledEntriesTest("TestTypeC");
			
			// add enty's using helper methods directly provided by ElevatorLogging (note: these static methods use the "real" sqlite database, not the testing one)
			//ElevatorLogging.arivedAt(1);
			//ElevatorLogging.arivedAt("one");
			//ElevatorLogging.emergencyBreak();
			//ElevatorLogging.emergencyBreak("myMessage");
			//ElevatorLogging.movingTo(2);
			//ElevatorLogging.movingTo("second floor");
			
			// add entry that should not work (because it's too large)
			tester.addTooLargeTypeEntrytest();
			
			// retrieve data in multiple ways (without filter; with ammount filter; with type filter; and with both filters (ammount & type))
			tester.retrieveTest();
			tester.retrieveLastFiveEntriesTest();
			tester.retrieveTestTypeATest();
			tester.retrieveLastThreeTestTypeBTest();
			
			// last test create's a mysterious risk to get a process killed.. dun dun dunnnnnnn (read the code that is 2 method calls away from this line to understand.. (omit Console.WriteLine() calls))
			tester.addEmptyEntryTest();

            Console.Read();
		}
		
		
		// \/ \/ \/ \/ \/ \/ \/ \/ a bit of testing code \/ \/ \/ \/ \/ \/ \/ \/
		public Tester() {
			el = new ElevatorLogging("DatastoreLibTestDatabase");
		}
		
		public void addFilledEntriesTest(string type) {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest addFilledEntryTest(): adding a filled entry to database.");
			byte[] randomBytes = System.Security.Cryptography.MD5.Create().ComputeHash(System.Text.Encoding.ASCII.GetBytes(DateTime.Now.ToString()));
			StringBuilder sb = new StringBuilder();
			for(int i=0; i<randomBytes.Length; i++) {
				sb.Append(randomBytes[i].ToString("X2"));
			}
			bool result = el.addEntry(type, sb.ToString());
			Console.WriteLine("DatastoreTest addFilledEntryTest(): result is: "+result.ToString());
		}
		
		public void addEmptyEntryTest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest addEmptyEntryTest(): adding a empty entry to database.");
			bool result = el.addEntry("", "");
			Console.WriteLine("DatastoreTest addEmptyEntryTest(): result is: "+result.ToString());
		}
		
		public void addTooLargeTypeEntrytest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest addTooLargeTypeEntrytest(): adding a entry to database that is too large.");
			bool result = el.addEntry("012345678901234567891", "<< 21 chars");
			Console.WriteLine("DatastoreTest addTooLargeTypeEntrytest(): result is: "+result.ToString()+" and should be False");
		}
		
		public void retrieveTest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest retrieveTest(): retrieving entry's from database. Printing them to console.");
			List<LoggingEntry> entryList = el.retrieveEntries();
			for(int i=0; i<entryList.Count; i++) {
				LoggingEntry entry = entryList[i];
				Console.WriteLine("DatastoreTest retrieveTest(): got: " + entry.rowID.ToString() + " - " + entry.type + " - " + entry.message + " - " + entry.timestamp);
			}
			Console.WriteLine("DatastoreTest retrieveTest(): done.");
		}
		
		public void retrieveLastFiveEntriesTest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest retrieveLastFiveEntriesTest(): retrieving last 5 entry's from database. Printing them to console.");
			List<LoggingEntry> entryList = el.retrieveEntries(5);
			for(int i=0; i<entryList.Count; i++) {
				LoggingEntry entry = entryList[i];
				Console.WriteLine("DatastoreTest retrieveTest(): got: " + entry.rowID.ToString() + " - " + entry.type + " - " + entry.message + " - " + entry.timestamp);
			}
			Console.WriteLine("DatastoreTest retrieveLastFiveEntriesTest(): done.");
		}
		
		public void retrieveTestTypeATest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest retrieveTestTypeATest(): retrieving all TestTypeA entry's from database. Printing them to console.");
			List<LoggingEntry> entryList = el.retrieveEntries("TestTypeA");
			for(int i=0; i<entryList.Count; i++) {
				LoggingEntry entry = entryList[i];
				Console.WriteLine("DatastoreTest retrieveTest(): got: " + entry.rowID.ToString() + " - " + entry.type + " - " + entry.message + " - " + entry.timestamp);
			}
			Console.WriteLine("DatastoreTest retrieveTestTypeATest(): done.");
		}
		
		public void retrieveLastThreeTestTypeBTest() {
			Console.WriteLine("");
			Console.WriteLine("DatastoreTest retrieveLastThreeTestTypeBTest(): retrieving last 3 TestTypeB entry's from database. Printing them to console.");
			List<LoggingEntry> entryList = el.retrieveEntries("TestTypeB", 3);
			for(int i=0; i<entryList.Count; i++) {
				LoggingEntry entry = entryList[i];
				Console.WriteLine("DatastoreTest retrieveTest(): got: " + entry.rowID.ToString() + " - " + entry.type + " - " + entry.message + " - " + entry.timestamp);
			}
			Console.WriteLine("DatastoreTest retrieveLastThreeTestTypeBTest(): done.");
		}
	}
}

